package com.xianbeiying.app.ckgl.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.bean.SysConfigBean;
import com.xianbeiying.app.ckgl.widght.NoScrollListView;

import java.util.List;

/**
 * 系统配置数据适配
 */
public class SystemConfigsAdapter extends BaseAdapter{
    private Context context;
    private List<SysConfigBean> systemConfigBeanArrayList;
    private Handler handler;

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    public SystemConfigsAdapter(Context context, List<SysConfigBean> systemConfigBeanArrayList) {
        this.context = context;
        this.systemConfigBeanArrayList=systemConfigBeanArrayList;
    }
    @Override
    public int getCount() {
        return systemConfigBeanArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return getItem(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_systems_config, parent, false);
            holder = new ViewHolder();
            holder.sc_last_line=(View) convertView.findViewById(R.id.sc_last_line);
            holder.iv_sy_delete=(ImageView) convertView.findViewById(R.id.iv_sy_delete);
            holder.iv_sc_img=(ImageView) convertView.findViewById(R.id.iv_sc_img);
            holder.tv_sc_type=(TextView) convertView.findViewById(R.id.tv_sc_type);
            holder.list_plant=(NoScrollListView) convertView.findViewById(R.id.list_plant);
            holder.tv_cname=(TextView) convertView.findViewById(R.id.tv_cname);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        holder.sc_last_line.setVisibility(View.VISIBLE);
        if((position+1)==systemConfigBeanArrayList.size()){
        holder.sc_last_line.setVisibility(View.VISIBLE);
        }
        holder.tv_cname.setText(systemConfigBeanArrayList.get(position).getConfigBean().get(0).getPlantName());
        holder.tv_sc_type.setText(systemConfigBeanArrayList.get(position).getConfigBean().get(0).getPlantType()==0?"育苗":"种植");
        holder.list_plant.setAdapter(new ItemSystemConfigsAdapter(context,systemConfigBeanArrayList.get(position).getConfigBean()));
        holder.iv_sy_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Message msg=handler.obtainMessage();
                msg.what=v.getId();
                msg.arg1=position;
                handler.sendMessage(msg);
            }
        });
        return convertView;
    }
    private class ViewHolder{
        ImageView iv_sc_img;
        TextView tv_sc_type;
        View sc_last_line;
        TextView tv_cname;
        NoScrollListView list_plant;
        ImageView iv_sy_delete;
    }
}
